package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.entity.History;
import com.igeek.collateral.entity.Inbound;
import com.igeek.collateral.entity.Outbound;
import com.igeek.collateral.mapper.CollateralMapper;
import com.igeek.collateral.mapper.HistoryMapper;
import com.igeek.collateral.mapper.InboundMapper;
import com.igeek.collateral.mapper.OutboundMapper;
import com.igeek.collateral.service.IHistoryService;
import com.igeek.collateral.vo.PageQueryVO;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-12
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements IHistoryService {
    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private InboundMapper inboundMapper;

    @Autowired
    private OutboundMapper outboundMapper;

    @Autowired
    private CollateralMapper collateralMapper;

    //存储进库的计数
    public static final String InNum="inNum::";

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public boolean addI(History history, Integer inboundId) {
        History history1 = history.setInboundId(inboundId);
        Inbound inbound = inboundMapper.selectById(inboundId);
        //同意之后将result改为已通过(这样前端页面的正常入库就)
        inbound.setResult(1);
        inboundMapper.updateById(inbound);
        //放入申请人
        history1.setApplyStaff(inbound.getApplyStaff());
        //放入审批人
        history1.setAuditStaff(inbound.getAuditStaff());
        //放入审批时间
        history1.setDate(inbound.getDate());
        Collateral collateral = collateralMapper.selectById(inbound.getCollateralId());
        //放入申请时间
        history1.setTime(collateral.getApplyTime());
        int i = historyMapper.insert(history1);
        return i>0?true:false;
    }




    @Override
    public Page<History> findPage(PageQueryVO vo) {

        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<History> page = new Page<>(currentPage , pageSize);
        //封装查询条件(根据历史编码(code)查询)
        LambdaQueryWrapper<History> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .like(StringUtils.hasLength(queryString),History::getCode,queryString);
        //执行分页查询条件
        Page<History> pageH = this.baseMapper.selectPage(page,lambdaQueryWrapper);
        return pageH;
    }

    @Override
    public boolean delete(Integer id) {
       LambdaQueryWrapper<History> lambdaQueryWrapper= new LambdaQueryWrapper<History>();
       lambdaQueryWrapper.eq(History::getId,id);
       int i=this.baseMapper.delete(lambdaQueryWrapper);
       return i>0?true:false;
    }

    @Override
    public boolean addO(History history, Integer outboundId) {
        History history1 = history.setOutboundId(outboundId);
        Outbound outbound = outboundMapper.selectById(outboundId);
        //同意之后将result改为已通过(这样前端页面的正常出库就行)
        outbound.setStatus(1);
        outboundMapper.updateById(outbound);
        //放入申请人
        history1.setApplyStaff(outbound.getApplyStaff());
        //放入审批人
        history1.setAuditStaff(outbound.getAuditStaff());
        //放入审批时间
        history1.setDate(outbound.getDate());
        Collateral collateral = collateralMapper.selectById(outbound.getCollateralId());
        //放入申请时间
        history1.setTime(collateral.getApplyTime());
        int i = historyMapper.insert(history1);
        return i>0?true:false;
    }
}
